package com.asr.testclient.view;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;

/**
 * 音频数据输入面板
 * 包含Base64文本框和文件选择功能
 */
public class InputPanel extends JPanel {
    private JTextArea base64TextArea;
    private JButton selectFileButton;
    private JButton submitButton;
    private JButton clearInputButton;
    private JScrollPane scrollPane;

    public InputPanel() {
        initComponents();
        layoutComponents();
        setupListeners();
    }

    private void initComponents() {
        base64TextArea = new JTextArea(10, 50);
        base64TextArea.setLineWrap(true);
        base64TextArea.setWrapStyleWord(false);
        scrollPane = new JScrollPane(base64TextArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        selectFileButton = new JButton("选择文件");
        submitButton = new JButton("提交识别");
        submitButton.setEnabled(false);
        clearInputButton = new JButton("清空输入");
    }

    private void layoutComponents() {
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createTitledBorder("音频数据输入"));

        // 文本区域
        add(scrollPane, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        buttonPanel.add(selectFileButton);
        buttonPanel.add(submitButton);
        buttonPanel.add(clearInputButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void setupListeners() {
        // 监听文本变化，自动启用/禁用提交按钮
        base64TextArea.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateSubmitButtonState();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateSubmitButtonState();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateSubmitButtonState();
            }

            private void updateSubmitButtonState() {
                boolean hasText = !base64TextArea.getText().trim().isEmpty();
                submitButton.setEnabled(hasText);
            }
        });
    }

    /**
     * 获取Base64数据
     */
    public String getBase64Data() {
        return base64TextArea.getText().trim();
    }

    /**
     * 设置Base64数据
     */
    public void setBase64Data(String data) {
        base64TextArea.setText(data);
        base64TextArea.setCaretPosition(0);
    }

    /**
     * 清空输入
     */
    public void clearInput() {
        base64TextArea.setText("");
    }

    /**
     * 设置提交按钮启用状态
     */
    public void setSubmitEnabled(boolean enabled) {
        submitButton.setEnabled(enabled);
    }

    /**
     * 获取选择文件按钮
     */
    public JButton getSelectFileButton() {
        return selectFileButton;
    }

    /**
     * 获取提交按钮
     */
    public JButton getSubmitButton() {
        return submitButton;
    }

    /**
     * 获取清空输入按钮
     */
    public JButton getClearInputButton() {
        return clearInputButton;
    }
}
